import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class CommandThread implements Runnable {

	private String command;

	public CommandThread(String cmd) {
		command = cmd;
	}

	@Override
	public void run() {
		List<String> input = new ArrayList<String>();
		StringTokenizer tokenizer = new StringTokenizer(command);
		while (tokenizer.hasMoreTokens()) {
			input.add(tokenizer.nextToken());
		}

		ProcessBuilder pb = new ProcessBuilder(input);
		// ProcessBuilder creates a process corresponding to the input command
		// now start the process
		BufferedReader br = null;
		try {
			Process proc = pb.start();
			// obtain the input and output streams
			InputStream is = proc.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			br = new BufferedReader(isr);
			// read what the process returned
			String line;
			while ((line = br.readLine()) != null) {
				System.out.println(line);
			}
			br.close();
		} catch (java.io.IOException ioe) {
			File errorLog = new File(
					"/Applications/Android development/workspace/OPS_Labb_1/res/errorLog.txt");
			try {
				PrintWriter out = new PrintWriter(errorLog);
				out.write(ioe.toString() + "\n");
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
}
